<template>
  <div class="wrapper">
    <!--  && step !== 2 -->
    <div slot="header" class="select—head" v-if="mode !== 'add'">
      <div v-if="step === 2">
        <!-- <el-menu :default-active="selectId" class="el-menu-demo" mode="horizontal" @select="handleSelect">
          
          <el-menu-item v-for="(item,index) in options" :key="index" :index="item.id">
            {{item.from_network_name}}-{{item.to_network_name}}
          </el-menu-item>
        </el-menu> -->
        <el-tag
          :type="item.id === selectId ? '' : 'info'"
          @click="handleClickTag(item)"
          class="my_tag"
          v-for="(item, index) in options"
          size="medium"
          :key="index"
          >{{ item.from_network_name }} - {{ item.to_network_name }}</el-tag
        >
      </div>
      <el-form v-else class="my-form">
        <el-form-item
          label="当前批次号"
          class="special"
          style="margin-bottom: 0px"
          prop="batch"
        >
          <el-select v-model="selectId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.batch"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="oper">
        <span v-if="step === 1">
          <el-button
            id="collect_complete"
            data-tracker="P4-7"
            v-permission="'waybillCollect_finish_update'"
            type="primary"
            plain
            size="small"
            @click="handleCommand(OPER.DELIVERY_FINISH)"
            >揽收完成</el-button
          >
        </span>
        <span v-show="step === 2">
          <el-button
            id="dispatch_depart"
            data-tracker="P4-12"
            v-permission="'dispatch_depart'"
            type="primary"
            plain
            size="small"
            @click="handleCommand(OPER.T_DEPART)"
            >发车</el-button
          >
          <el-button
            id="dispatch_arrive"
            data-tracker="P4-13"
            v-permission="'dispatch_arrival'"
            type="primary"
            plain
            size="small"
            @click="handleCommand(OPER.T_ARRIVAL)"
            >到达</el-button
          >
        </span>
        <span v-show="step === 3">
          <el-button
            id="distribution_complete"
            data-tracker="P4-20"
            v-permission="'waybillDistribution_finish_update'"
            type="primary"
            plain
            size="small"
            @click="handleCommand(OPER.D_FINISH)"
            >配送完成</el-button
          >
        </span>
        <span v-show="step === 5">
          <el-button
            id="sign_receipt_or_box_recycle"
            data-tracker="P4-30"
            v-permission="'waybill_receipt_receipt_sign'"
            type="primary"
            plain
            size="small"
            @click="handleReceiptSign"
            >签收</el-button
          >
        </span>
        <template v-if="step === 6 &&( Typelist == 1 || Typelist == 3 ) ">
          <el-button type="primary" plain size="small" @click="handleRecycleSign">签收</el-button>
        </template>
        <el-dropdown
          :key="step + ''"
          v-show="isHavePermission(getButtonNames())"
          @command="handleCommand"
          :hide-on-click="false"
          style="margin-left: 10px"
        >
          <el-button type="primary" size="small" plain>
            更多操作<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown" v-if="step === 1">
            <el-dropdown-item
              data-tracker="P4-8"
              v-permission="'waybillCollect_update'"
              class="select_wrap_drop"
              style=""
              :command="OPER.EDIT"
              >编辑</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-9"
              v-permission="'waybill_collect_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ADD"
              >新增</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-10"
              v-permission="'waybillCollect_cancelFinish_update'"
              class="select_wrap_drop"
              style=""
              :command="OPER.DELIVERY_CANCEL"
              >取消揽收完成</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-11"
              v-permission="'abnormal_record_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ABNORMAL"
              >提报异常</el-dropdown-item
            >
          </el-dropdown-menu>
          <el-dropdown-menu slot="dropdown" v-if="step === 2">
            <el-dropdown-item
              data-tracker="P4-14"
              v-permission="'dispatch_update'"
              class="select_wrap_drop"
              style=""
              :command="OPER.EDIT"
              >编辑</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-15"
              v-permission="'dispatch_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ADD"
              >新增</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-16"
              v-permission="'dispatch_depart_cancel'"
              class="select_wrap_drop"
              style=""
              :command="OPER.T_DEPART_CANCEL"
              >取消发车</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-17"
              v-permission="'dispatch_arrival_cancel'"
              class="select_wrap_drop"
              style=""
              :command="OPER.T_ANCELLATION"
              >取消到达</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-18"
              v-permission="'dispatch_loaded_cancel'"
              class="select_wrap_drop"
              style=""
              :command="OPER.T_LOAD_CANCEL"
              >删除</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-19"
              v-permission="'abnormal_record_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ABNORMAL"
              >提报异常</el-dropdown-item
            >
          </el-dropdown-menu>
          <el-dropdown-menu slot="dropdown" v-if="step === 3">
            <el-dropdown-item
              data-tracker="P4-21"
              v-permission="'waybillDistribution_amend_update'"
              class="select_wrap_drop"
              style=""
              :command="OPER.EDIT"
              >编辑</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-22"
              v-permission="'waybillDistribution_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ADD"
              >新增</el-dropdown-item
            >
            <template v-if="distributionSwitch">
              <el-dropdown-item
                data-tracker="P4-23"
                v-permission="'waybillDistribution_cancelFinish_update'"
                class="select_wrap_drop"
                style=""
                :command="OPER.D_CANCEL"
                >取消配送完成</el-dropdown-item
              >
            </template>
            <el-dropdown-item
              data-tracker="P4-24"
              v-permission="'abnormal_record_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ABNORMAL"
              >提报异常</el-dropdown-item
            >
          </el-dropdown-menu>
          <el-dropdown-menu slot="dropdown" v-if="step === 4">
            <el-dropdown-item
              data-tracker="P4-25"
              v-permission="'waybillSign_update'"
              class="select_wrap_drop"
              style=""
              :command="OPER.EDIT"
              >编辑</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-26"
              v-permission="'waybillSign_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ADD"
              >新增</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-27"
              v-permission="'waybillSign_cancelSign_update'"
              class="select_wrap_drop"
              style=""
              :command="OPER.SIGN_CANCEL"
              >取消签收</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-28"
              v-permission="'waybillSign_cancelSign_update'"
              class="select_wrap_drop"
              style=""
              :command="OPER.SIGN_REJECT_CANCEL"
              >取消拒收</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-29"
              v-permission="'abnormal_record_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ABNORMAL"
              >提报异常</el-dropdown-item
            >
          </el-dropdown-menu>
          <el-dropdown-menu slot="dropdown" v-if="step === 5">
            <el-dropdown-item
              data-tracker="P4-32"
              v-permission="'waybill_receipt_update'"
              class="select_wrap_drop"
              style=""
              :command="OPER.EDIT"
              >编辑</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-33"
              v-permission="'waybill_receipt_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ADD"
              >新增</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-34"
              v-permission="'abnormal_record_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ABNORMAL"
              >提报异常</el-dropdown-item
            >
          </el-dropdown-menu>
          <el-dropdown-menu slot="dropdown" v-if="step === 6">
            <el-dropdown-item
              data-tracker="P4-35"
              v-permission="'waybill_box_recyle_update'"
              class="select_wrap_drop"
              style=""
              :command="OPER.EDIT"
              >编辑</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-36"
              v-permission="'waybill_box_recyle_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ADD"
              >新增</el-dropdown-item
            >
            <el-dropdown-item
              data-tracker="P4-37"
              v-permission="'abnormal_record_create'"
              class="select_wrap_drop"
              style=""
              :command="OPER.ABNORMAL"
              >提报异常</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
        <!-- <el-tooltip class="item" effect="dark" content="编辑" placement="left-start">
          <el-button style="margin-left:40px" type="primary" @click="handleEdit" icon="el-icon-edit" plain size="medium"
            class="btn" circle></el-button>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="添加" placement="left-start">
          <el-button type="primary" @click="handleAdd" icon="el-icon-plus" plain size="medium" class="btn" circle>
          </el-button>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="异常" placement="left-start">
          <el-button type="danger" @click="handleAbnormal" icon="el-icon-warning-outline" plain size="medium"
            class="btn" circle>
          </el-button>
        </el-tooltip>
        <el-dropdown v-if="step === 1 || step === 2 || step === 3 || step === 4" @command="handleCommand"
          :hide-on-click="false" style="margin-left:20px">
          <span class="el-dropdown-link">
            更多操作<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown" v-if="step === 1">
            <el-dropdown-item :command="OPER.DELIVERY_FINISH">待创建干线</el-dropdown-item>
            <el-dropdown-item :command="OPER.DELIVERY_CANCEL">取消待创建干线</el-dropdown-item>
          </el-dropdown-menu>
          <el-dropdown-menu slot="dropdown" v-if="step === 2">
            <el-dropdown-item :command="OPER.T_DEPART">发车</el-dropdown-item>
            <el-dropdown-item :command="OPER.T_ARRIVAL">到达</el-dropdown-item>
            <el-dropdown-item :command="OPER.T_DEPART_CANCEL">取消发车</el-dropdown-item>
            <el-dropdown-item :command="OPER.T_ANCELLATION">取消到达</el-dropdown-item>
            <el-dropdown-item :command="OPER.T_LOAD_CANCEL">删除</el-dropdown-item>
          </el-dropdown-menu>
          <el-dropdown-menu slot="dropdown" v-if="step === 3">
            <el-dropdown-item :command="OPER.D_FINISH">配送完成 </el-dropdown-item>
            <el-dropdown-item :command="OPER.D_CANCEL">取消配送完成</el-dropdown-item>
          </el-dropdown-menu>
          <el-dropdown-menu slot="dropdown" v-if="step === 4">
            <el-dropdown-item :command="OPER.SIGN_CANCEL">取消签收</el-dropdown-item>
            <el-dropdown-item :command="OPER.SIGN_REJECT_CANCEL">取消拒收</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown> -->
      </div>
    </div>
    <!-- updateMode 干线使用 更新当前mode -->
    <!-- updateFinish 更新运单状态 各个流程对应的状态 -->
    <!-- update 获取batch list -->
    <component
      v-if="mode && mode !== 'add'"
      key="1"
      :mode="mode"
      :ids="ids"
      @update="handleUpdate"
      @updateMode="handleUpdateMode"
      @updateFinish="handleUpdateFinish"
      :id="Number(selectId)"
      :is="currentView"
      :waybillInfo="waybillInfo"
      type="process"
      :is_incubator="is_incubator"
      :businessType="businessType"
      @transmit="getMessage"
    >
    </component>
    <component
      v-else-if="mode"
      :mode="mode"
      :waybillInfo="waybillInfo"
      :is_incubator="is_incubator"
      key="2"
      :ids="ids"
      @update="handleUpdate"
      :lastToNetwork="lastToNetwork"
      type="process"
      @updateFinish="handleUpdateFinish"
      :is="currentView"
      :businessType="businessType"
    >
    </component>
    <CheckInfo
      :showDialog="checkDialogVisible"
      @ok="deliveryFinish"
      @close="handleCloseCheckDialog"
      :err_list="err_list"
    ></CheckInfo>

    <!-- <div @click="handleSkipNode" class="node-skip">
      跳过
    </div> -->
  </div>
</template>

<script>
import SingleDelivery from "./single/singleDelivery.vue";
import SingleTrunkLine from "./trunkLine2.vue";
import SingleDistribution from "./single/singleDistribution.vue";
import SingleSignfor from "./single/singleSignfor.vue";
import SingleAddReceipt from "./single/singleAddReceipt.vue";
import SingleRecycle from "./single/singleRecycle.vue";
import AbnormalModal from "./abnormalModal.vue";
import CheckInfo from "./checkInfo.vue";

import {
  WaybillDetailFn,
  collectBatchList,
  dispatchBatchList,
  distributionBatchList,
  signBatchList,
  receiptBatchList,
  recyleBatchList,
  deliveryFinishFn,
  deliveryCancelFn,
  DistributionSuccessFn,
  DistributionCancelFn,
  SignforCancelFn,
  TrunklineDepartFn,
  TrunklineArrivalFn,
  TrunklineDepartCancelFn,
  TrunklineLoadCancelFn,
  waybillSkipNode,
  getTransportsList,
  SignforCountFn,
  dispatchcancel,
  ReceiptSign,
  RecycleSignFn,
} from "@/api/waybill";
import FinishTime from "./finishTime.vue";
import { isHavePermission } from "@/utils/mUtils";

export default {
  name: "selectWrap",
  components: {
    FinishTime,
    SingleDelivery,
    SingleTrunkLine,
    SingleDistribution,
    SingleSignfor,
    SingleAddReceipt,
    SingleRecycle,
    AbnormalModal,
    CheckInfo,
  },
  props: ["step", "waybillInfo", "is_incubator", "businessType"],
  data() {
    return {
      options: [],
      selectId: "",
      mode: "",
      viewArr: [
        "",
        "SingleDelivery",
        "SingleTrunkLine",
        "SingleDistribution",
        "SingleSignfor",
        "SingleAddReceipt",
        "SingleRecycle",
      ],
      currentView: "SingleDelivery",
      waybillId: "",
      OPER: {
        DELIVERY_FINISH: "1-1",
        DELIVERY_CANCEL: "1-2",
        T_DEPART: "2-1",
        T_ARRIVAL: "2-2",
        T_DEPART_CANCEL: "2-3",
        T_LOAD_CANCEL: "2-4",
        T_ANCELLATION: "2-5",
        D_FINISH: "3-1",
        D_CANCEL: "3-2",
        SIGN_CANCEL: "4-1",
        SIGN_REJECT_CANCEL: "4-2",
        EDIT: "edit",
        ADD: "add",
        ABNORMAL: "abnormal",
      },
      showOperArr: [1, 3],
      lastToNetwork: "",
      checkDialogVisible: false,
      err_list: [],
      finish_time: "",
      distributionSwitch: false,
      Typelist:"",
    };
  },
  watch: {
    step: {
      handler: function (newValue) {
        this.selectId = "";
        this.mode = "";
        this.getOptions();
        this.currentView = this.viewArr[newValue];
      },
      // immediate: true,
    },
    selectId(val) {
      // 切换id是 模式初始化为详情
      if (val !== "") {
        this.mode = "singleDetail";
      }
    },
  },
  async created() {
    this.waybillId = this.$route.query.id;
    this.ids = [Number(this.$route.query.id)];
  },
  mounted() {
    this.getOptions();
    this.distributionSwitch = this.getSwitch();
  },
  async updated() {},
  methods: {
    getSwitch() {
      // 0-自动配送完成 1-正常
      const item = (this.$store.state.user.setting_list || []).find(
        (item) => item.item_name === "distribution_finish_switch"
      );
      return item.item_value === "1";
    },
    isHavePermission(val) {
      return isHavePermission(val);
    },
    getButtonNames() {
      // console.log(this.step,'ssssssssssstep');
      if (this.step === 1) {
        return [
          "waybillCollect_update",
          "waybill_collect_create",
          "waybillCollect_cancelFinish_update",
          "abnormal_record_create",
        ];
      } else if (this.step === 2) {
        return [
          "dispatch_update",
          "dispatch_create",
          "dispatch_depart_cancel",
          "dispatch_arrival_cancel",
          "dispatch_loaded_cancel",
          "abnormal_record_create",
        ];
      } else if (this.step === 3) {
        return [
          "waybillDistribution_amend_update",
          "waybillDistribution_create",
          "waybillDistribution_cancelFinish_update",
          "abnormal_record_create",
        ];
      } else if (this.step === 4) {
        return [
          "waybillSign_update",
          "waybillSign_create",
          "waybillSign_cancelSign_update",
          "abnormal_record_create",
        ];
      } else if (this.step === 5) {
        return [
          "waybill_receipt_update",
          "waybill_receipt_create",
          "abnormal_record_create",
        ];
      } else if (this.step === 6) {
        return [
          "waybill_box_recyle_update",
          "waybill_box_recyle_create",
          "abnormal_record_create",
        ];
      }
    },
    handleEdit() {
      this.mode = "singleEdit";
    },
    handleAdd() {
      this.mode = "add";
    },
    handleClickTag(item) {
      // console.log(item);
      this.selectId = item.id;
    },
    handleAbnormal() {
      const { waybillId, selectId, step } = this;
      this.$msgbox({
        title: "提交异常",
        message: (
          <AbnormalModal
            key={Math.random()}
            step={step}
            waybillIds={[waybillId]}
            nodeId={selectId}
            style="width:600px"
          />
        ),
        // center: true,
        customClass: "temperature_msg",
        showConfirmButton: false,
        showCancelButton: false,
        closeOnClickModal: false,
        showClose: true,
      });
    },
    /**
     *
     * @param {*} flag true-获取batchlist更新  false-更新状态为详情
     */
    handleUpdate(isUpdateSelectID = true) {
      // this.$emit('changeStatus')
      // if (flag) {
      //   this.getOptions(isUpdateSelectID);
      // }else{
      //   this.mode = "singleDetail";
      // }
      /**
       * 写干线优化需求更新（修改干线单的出发网点目的网点上边的需要更新，但selectid不更新）
       */
      this.getOptions(isUpdateSelectID);
      if (!isUpdateSelectID) {
        this.mode = "singleDetail";
      }
    },
    handleUpdateMode(mode) {
      this.mode = mode;
    },
    handleUpdateFinish() {
      this.$emit("updateFinish");
    },
    handleReceiptSign() {
      const title = "签收";
      this.$msgbox({
        message: (
          <FinishTime
            title={title}
            key={Date.now()}
            hanldeOk={this.handleReceiptFinish}
          />
        ),
        customClass: "temperature_msg",
        showConfirmButton: false,
        showCancelButton: false,
        showClose: true,
        title,
      });
    },
    handleReceiptFinish(time) {
      ReceiptSign({
        id: [this.selectId * 1],
        sign_time: time,
        sign_id: this.$store.state.user.user_id + "",
        sign_name: this.$store.state.user.nickname,
      }).then((res) => {
        if (res.code === 10000) {
          this.$message({
            message: "回单签收完成",
            type: "success",
          });
          this.handleUpdateFinish();
          this.handleUpdate(false);
        }
      });
    },
    handleRecycleSign() {
      const title = "签收";
      this.$msgbox({
        message: (
          <FinishTime
            title={title}
            key={Date.now()}
            hanldeOk={this.handleRecycleFinish}
          />
        ),
        customClass: "temperature_msg",
        showConfirmButton: false,
        showCancelButton: false,
        showClose: true,
        title,
      });
    },
    handleRecycleFinish(time) {
      RecycleSignFn({
        id: [this.selectId * 1],
        sign_time: time,
        sign_id: this.$store.state.user.user_id + "",
        sign_name: this.$store.state.user.nickname,
      }).then((res) => {
        if (res.code === 10000) {
          this.$message({
            message: "回箱签收完成",
            type: "success",
          });
          this.handleUpdateFinish();
          this.handleUpdate(false);
        }
      });
    },
    handleCloseCheckDialog() {
      this.checkDialogVisible = false;
    },
    handleCommand(val) {
      const {
        DELIVERY_FINISH,
        DELIVERY_CANCEL,
        D_FINISH,
        D_CANCEL,
        SIGN_CANCEL,
        T_DEPART,
        T_ARRIVAL,
        T_DEPART_CANCEL,
        T_LOAD_CANCEL,
        T_ANCELLATION,
        SIGN_REJECT_CANCEL,
        EDIT,
        ADD,
        ABNORMAL,
      } = this.OPER;
      switch (val) {
        case EDIT:
          this.handleEdit();
          break;
        case ADD:
          this.handleAdd();
          break;
        case ABNORMAL:
          this.handleAbnormal();
          break;
        case DELIVERY_FINISH:
          const title = "揽收完成";
          this.$msgbox({
            message: (
              <FinishTime
                title={title}
                key={Date.now()}
                hanldeOk={this.handleDeliveryFinish}
              />
            ),
            customClass: "temperature_msg",
            showConfirmButton: false,
            showCancelButton: false,
            showClose: true,
            title,
          });
          break;
        case DELIVERY_CANCEL:
          deliveryCancelFn({ id_list: [Number(this.selectId)] }).then((res) => {
            if (res.code === 10000) {
              this.$message({
                message: "取消成功",
                type: "success",
              });
              this.handleUpdateFinish();
              this.handleUpdate(false);
              this.updateDetail();
              // this.$forceUpdate()
              // this.$router.push({
              //   name: "waybillDetail",
              //   query: { id:this.$route.query.id },
              // })
              // location.reload()
            }
          });
          break;
        case T_DEPART:
          const title1 = "发车";
          this.$msgbox({
            message: (
              <FinishTime
                title={title1}
                key={Date.now()}
                hanldeOk={this.handleTrunkLineDepart}
              />
            ),
            customClass: "temperature_msg",
            showConfirmButton: false,
            showCancelButton: false,
            showClose: true,
            title: title1,
          });
          break;
        case T_ARRIVAL:
          const title2 = "到达";
          this.$msgbox({
            message: (
              <FinishTime
                title={title2}
                key={Date.now()}
                hanldeOk={this.handleTrunkLineArrival}
              />
            ),
            customClass: "temperature_msg",
            showConfirmButton: false,
            showCancelButton: false,
            showClose: true,
            title: title2,
          });
          break;
        case T_DEPART_CANCEL:
          TrunklineDepartCancelFn({ id: this.selectId }).then((res) => {
            if (res.code === 10000) {
              this.$message({
                message: "取消发车完成",
                type: "success",
              });
              this.handleUpdateFinish();
              this.handleUpdate(false);
              this.updateDetail();
            }
          });
          break;
        case T_LOAD_CANCEL:
          TrunklineLoadCancelFn({ id: this.selectId }).then((res) => {
            if (res.code === 10000) {
              this.$message({
                message: "删除完成",
                type: "success",
              });
              this.handleUpdateFinish();
              this.handleUpdate();
            }
          });
          break;
        case T_ANCELLATION:
          dispatchcancel({
            id: this.selectId,
          }).then((res) => {
            if (res.code === 10000) {
              this.$message({
                message: "取消到达完成",
                type: "success",
              });
              this.handleUpdateFinish();
              this.handleUpdate(false);
              this.updateDetail();
            }
          });
          break;
        case D_FINISH:
          const title3 = "配送完成";
          this.$msgbox({
            message: (
              <FinishTime
                title={title3}
                key={Date.now()}
                hanldeOk={this.handleDistributionFinish}
              />
            ),
            customClass: "temperature_msg",
            showConfirmButton: false,
            showCancelButton: false,
            showClose: true,
            title: title3,
          });
          break;
        case D_CANCEL:
          DistributionCancelFn({ ids: [Number(this.selectId)] }).then((res) => {
            if (res.code === 10000) {
              this.$message({
                message: "取消配送成功",
                type: "success",
              });
              this.handleUpdateFinish();
              this.handleUpdate(false);
              this.updateDetail();
              // this.$router.push({
              //   name: "waybillDetail",
              //   query: { id:this.$route.query.id },
              // })
              // location.reload()
            } else {
              this.$alert(res.message, "错误提示");
            }
          });
          break;
        case SIGN_CANCEL:
          this.confirmSignCancel("0");
          break;
        case SIGN_REJECT_CANCEL:
          this.confirmSignCancel("1");
          break;
        default:
          break;
      }
      // this.handleUpdateFinish();
    },
    async confirmSignCancel(type) {
      // type '0'取消签收 '1'取消拒收
      // const title = type === '0' ? '该运单下存在其它签收单，是否一起取消？' : '该运单下存在其它拒收单，是否一起取消？';
      const res = await SignforCountFn({ id: this.selectId });
      const count = res.data.count;
      // 签收单id关联运单下的签收单个数<=1，不弹框提示
      if (count <= 1) {
        this.cancelSign(false);
        return;
      }
      let is_cancel_all;
      this.$confirm("该运单下存在其它签收/拒收单，是否一起取消？", "提示", {
        confirmButtonText: "全部取消",
        cancelButtonText: "取消当前",
        distinguishCancelAndClose: true,
        type: "warning",
      })
        .then(() => {
          is_cancel_all = true;
          this.cancelSign(is_cancel_all);
        })
        .catch((action) => {
          if (action === "cancel") {
            is_cancel_all = false;
            this.cancelSign(is_cancel_all);
          }
        });
    },
    cancelSign(is_cancel_all) {
      SignforCancelFn({ id: Number(this.selectId), is_cancel_all }).then(
        (res) => {
          if (res.code === 10000) {
            this.$message({
              message: "取消完成",
              type: "success",
            });
            this.handleUpdate();
            this.handleUpdateFinish();
            // this.mode='add'
          } else {
            this.$message({
              message: res.message,
              type: "warning",
            });
          }
        }
      );
    },
    async getOptions(isUpdateSelectID = true) {
      if (this.step === 0) return;
      this.waybillId = this.$route.query.id;
      if (this.step === 1) {
        const res = await collectBatchList({ Id: this.waybillId });
        this.options = res.data;
      } else if (this.step === 2) {
        const res = await dispatchBatchList({ waybill_id: this.waybillId });
        const data = res.data.options;
        this.options = data;
        // &&
        // data.map((item) => {
        //   item.router=
        // });
        if (data) {
          const last = data[data.length - 1];
          this.lastToNetwork = {
            to_network_id: last.to_network_id,
            to_network_name: last.to_network_name,
          };
        }
      } else if (this.step === 3) {
        const res = await distributionBatchList({ Id: this.waybillId });
        this.options = res.data;
      } else if (this.step === 4) {
        const res = await signBatchList({ Id: this.waybillId });
        this.options = res.data;
      } else if (this.step === 5) {
        const res = await receiptBatchList({ waybill_id: this.waybillId });
        const data = res.data.options || [];
        this.options = data.map((item) => {
          return { id: item.key, batch: item.Value };
        });
      } else if (this.step === 6) {
        const res = await recyleBatchList({ waybill_id: this.waybillId });
        const data = res.data.options || [];
        this.options = data.map((item) => {
          return { id: item.key, batch: item.value };
        });
      }
      if (isUpdateSelectID) {
        this.selectId = "";
        if (this.options && this.options.length > 0) {
          const index = this.options.length - 1;
          setTimeout(() => {
            this.selectId = this.options[index].id;
          });
          this.mode = "singleDetail";
        } else {
          this.selectId = "";
          this.mode = "add";
        }
      }
    },
    updateDetail() {
      const nowId = this.selectId;
      this.selectId = "";
      setTimeout(() => {
        this.selectId = nowId;
      });
    },
    handleDeliveryFinish(time) {
      this.finish_time = time;
      this.deliveryFinish();
    },
    deliveryFinish() {
      this.checkDialogVisible = false;
      deliveryFinishFn({
        id_list: [Number(this.selectId)],
        finish_time: this.finish_time,
        source: "pc",
      }).then((res) => {
        if (res.code === 10000) {
          if (res.data.err_list) {
            this.checkDialogVisible = true;
            this.err_list = res.data.err_list;
          } else {
            this.$message({
              message: "揽收完成",
              type: "success",
            });
            this.handleUpdateFinish();
            this.handleUpdate(false);
            this.updateDetail();
          }
        }
      });
    },
    async handleSkipNode() {
      const stepToType = [, "1", "3", "4", "7", "5", "8"];

      const result = await waybillSkipNode({
        data: [
          {
            node: stepToType[this.step],
            waybill_id: this.waybillInfo.id,
          },
        ],
      });

      if (result.code === 10000) {
        this.$message({
          type: "success",
          message: `跳过节点成功!`,
        });
        this.handleUpdate();
        this.handleUpdateFinish();
      }
    },
    handleDistributionFinish(time) {
      DistributionSuccessFn({
        ids: [Number(this.selectId)],
        finish_time: time,
      }).then((res) => {
        if (res.code === 10000) {
          this.$message({
            message: "配送完成",
            type: "success",
          });
          this.handleUpdateFinish();
          this.handleUpdate(false);
          this.updateDetail();
          // setTimeout(()=>{
          //     this.$router.push({
          //       name: "waybillDetail",
          //       query: { id:this.$route.query.id },
          //     })
          //     location.reload()
          // },1000)
        } else {
          this.$alert(res.message, "错误提示");
        }
      });
    },
    handleTrunkLineDepart(time) {
      TrunklineDepartFn({ id: this.selectId, departure_time: time }).then(
        (res) => {
          if (res.code === 10000) {
            this.$message({
              message: "发车完成",
              type: "success",
            });
            this.handleUpdateFinish();
            this.handleUpdate(false);
            this.updateDetail();
          }
        }
      );
    },
    handleTrunkLineArrival(time) {
      TrunklineArrivalFn({ id: this.selectId, arrival_time: time }).then(
        (res) => {
          if (res.code === 10000) {
            this.$message({
              message: "到达完成",
              type: "success",
            });
            this.handleUpdateFinish();
            this.handleUpdate(false);
            this.updateDetail();
          }
        }
      );
    },
    getMessage(data) {
      this.Typelist = data
    }
  },
};
</script>
<style lang="scss" scoped>
.wrapper {
  position: relative;

  .my-form {
    width: 50%;
  }

  .oper {
    // position: absolute;
    // top: 20px;
    // right: 20px;
  }

  .el-dropdown-link {
    cursor: pointer;
    color: #409eff;
  }

  .el-icon-arrow-down {
    font-size: 12px;
  }

  .select—head {
    padding: 20px;
    padding-bottom: 0;
    display: flex;
    justify-content: space-between;
  }

  .node-skip {
    width: 48px;
    height: 34px;
    cursor: pointer;
    line-height: 34px;
    position: fixed;
    top: 480px;
    right: 0px;
    background: white;
    border: solid 1px lightblue;
    padding-left: 10px;
    z-index: 100;
    border-radius: 6px 0 0 6px;
  }

  .my_tag {
    margin-right: 20px;
  }
}
</style>
<style lang="less">
.safari_table .el-table__body {
  width: 100% !important;
  table-layout: fixed !important;
}

.select_wrap_drop {
  font-size: 14px !important;
  padding: 3px 10px !important;
  min-width: 90px;
}
</style>
